package org.asiainfo.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import org.asiainfo.common.core.constant.TenantConstants;
import org.asiainfo.common.core.exception.ServiceException;
import org.asiainfo.common.core.utils.SpringUtils;
import org.asiainfo.common.core.utils.StringUtils;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.system.domain.SysTenant;
import org.asiainfo.system.domain.vo.SysTenantPackageVo;
import org.asiainfo.system.domain.bo.SysTenantPackageBo;
import org.asiainfo.system.domain.SysTenantPackage;
import org.asiainfo.system.service.SysTenantPackageService;
import org.asiainfo.system.service.SysTenantService;
import org.springframework.stereotype.Service;
import org.asiainfo.common.core.utils.MapstructUtils;
import lombok.extern.slf4j.Slf4j;
import cn.hutool.core.util.ObjectUtil;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.asiainfo.system.mapper.SysTenantPackageMapper;
import com.mybatisflex.core.paginate.Page;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 租户套餐表(SysTenantPackage)表服务实现类
 *
 * @author dotor-ww
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SysTenantPackageServiceImpl extends ServiceImpl<SysTenantPackageMapper, SysTenantPackage> implements SysTenantPackageService {

    /**
     * 构建查询条件
     *
     * @param sysTenantPackageBo 套餐信息
     * @return 构建结果
     */
    private QueryWrapper buildQueryWrapper(SysTenantPackageBo sysTenantPackageBo) {
        QueryWrapper query = query();
        query.like(SysTenantPackage::getPackageName, sysTenantPackageBo.getPackageName(), StringUtils.isNotBlank(sysTenantPackageBo.getPackageName()));
        query.eq(SysTenantPackage::getStatus, sysTenantPackageBo.getStatus(), ObjectUtil.isNotNull(sysTenantPackageBo.getStatus()));
        query.orderBy(SysTenantPackage::getPackageId, true);
        return query;
    }

    /**
     * 查询
     *
     * @param sysTenantPackageBo 筛选条件
     * @return 查询结果
     */
    @Override
    public List<SysTenantPackageVo> queryList(SysTenantPackageBo sysTenantPackageBo) {
        QueryWrapper query = buildQueryWrapper(sysTenantPackageBo);
        return listAs(query, SysTenantPackageVo.class);
    }

    /**
     * 通过packageId查询单条数据
     *
     * @param packageId 主键
     * @return 实例对象
     */
    @Override
    public SysTenantPackageVo queryById(Long packageId) {
        return getOneAs(query().eq(SysTenantPackage::getPackageId, packageId), SysTenantPackageVo.class);
    }

    /**
     * 分页查询
     *
     * @param sysTenantPackageBo 筛选条件
     * @param pageQuery          分页对象
     * @return 查询结果
     */
    @Override
    public TableDataInfo<SysTenantPackageVo> queryPageList(SysTenantPackageBo sysTenantPackageBo, PageQuery pageQuery) {
        QueryWrapper query = buildQueryWrapper(sysTenantPackageBo);
        Page<SysTenantPackageVo> page = pageAs(pageQuery.buildPage(), query, SysTenantPackageVo.class);
        return TableDataInfo.build(page);
    }

    /**
     * 新增数据
     *
     * @param sysTenantPackageBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insertByBo(SysTenantPackageBo sysTenantPackageBo) {
        SysTenantPackage add = MapstructUtils.convert(sysTenantPackageBo, SysTenantPackage.class);
        if (ObjectUtil.isNull(add)) {
            throw new ServiceException("新增套餐解析失败!");
        }
        List<Long> menuIds = Arrays.asList(sysTenantPackageBo.getMenuIds());
        if (CollUtil.isNotEmpty(menuIds)) {
            add.setMenuIds(StringUtils.join(menuIds, ", "));
        } else {
            add.setMenuIds("");
        }
        return save(add);
    }

    /**
     * 修改数据
     *
     * @param sysTenantPackageBo 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean updateByBo(SysTenantPackageBo sysTenantPackageBo) {
        SysTenantPackage update = MapstructUtils.convert(sysTenantPackageBo, SysTenantPackage.class);
        if (ObjectUtil.isNull(update)) {
            throw new ServiceException("修改套餐解析失败!");
        }
        List<Long> menuIds = Arrays.asList(sysTenantPackageBo.getMenuIds());
        if (CollUtil.isNotEmpty(menuIds)) {
            update.setMenuIds(StringUtils.join(menuIds, ", "));
        } else {
            update.setMenuIds("");
        }
        return updateById(update);
    }

    /**
     * 通过主键删除数据
     *
     * @param packageIds 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> packageIds) {
        boolean exists = SpringUtils.getBean(SysTenantService.class).exists(query().in(SysTenant::getPackageId, packageIds));
        if (exists) {
            throw new ServiceException("该租户套餐已被使用!");
        }
        return removeByIds(packageIds);
    }

    /**
     * 修改套餐状态
     *
     * @param sysTenantPackageBo 套餐信息
     * @return 修改结果
     */
    @Override
    public Boolean updatePackageStatus(SysTenantPackageBo sysTenantPackageBo) {
        SysTenantPackage sysTenantPackage = MapstructUtils.convert(sysTenantPackageBo, SysTenantPackage.class);
        return updateById(sysTenantPackage);
    }

    /**
     * 查询租户套餐下拉列表
     *
     * @return 租户套餐
     */
    @Override
    public List<SysTenantPackageVo> selectList() {
        return listAs(query().eq(SysTenantPackage::getStatus, TenantConstants.NORMAL), SysTenantPackageVo.class);
    }
}
